package com.example.user.controller;


import annotation.RequireRoles;
import com.example.user.service.UserServiceImpl;
import entity.PageParam;
import entity.Rsp;
import entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户管理
 */

@RestController
@Api(value = "用户模块接口",description="用户模块接口")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserServiceImpl userService;



    @PostMapping("/searchSomeUserByPage")
    @RequireRoles(roles = {"admin","super_admin"},index = 2)
    @ApiOperation("查询所有用户 支持 多条件 分页 排序 查询")
    public Rsp searchSomeByPage(@RequestBody PageParam<UserEntity> userEntityPageParam,
                      @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        log.info("userId:{}",userId);
        log.info("roleId:{}",roleSign);

        return Rsp.success(userService.searchSomeByPage(userEntityPageParam));
    }


    @RequireRoles(roles = {"super_admin","admin"},index = 2)
    @PostMapping("/addOneUser")
    public Rsp addOne(@RequestBody @Valid UserEntity userEntity, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        return userService.addOne(userEntity);
    }


    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/deleteOneUserTrue")
    public Rsp deleteOneUserTrue(@RequestBody UserEntity userEntity, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        return userService.deleteOneTrue(userEntity.getId());
    }

    @RequireRoles(roles = {"super_admin","admin"},index = 2)
    @PostMapping("/updateUser")
    public Rsp updateUser(@RequestBody UserEntity userEntity, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){



        return userService.update(userEntity);
    }

    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/deleteUserLogical")
    public Rsp deleteUserLogical(@RequestBody UserEntity userEntity, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){



        return userService.deleteLogical(userEntity);
    }

    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/recoverUserLogical")
    public Rsp recoverUserLogical(@RequestBody UserEntity userEntity, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){



        return userService.recoverLogical(userEntity);
    }


    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/deleteBatchUserTrue")
    public Rsp deleteBatchUserTrue(@RequestBody List<String> ids, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){



        return userService.deleteBatchTrue(ids);
    }

    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/deleteBatchUserLogical")
    public Rsp deleteBatchUserLogical(@RequestBody List<String> ids, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){



        return userService.deleteBatchLogical(ids);
    }


}
